package cn.lls.controller.ca;


import cn.lls.base.result.R;
import cn.lls.base.util.MD5Util;
import cn.lls.entity.company.CompanyAccountEntity;
import cn.lls.req.company.CAAccountReq;
import cn.lls.req.company.CompanyReq;
import cn.lls.service.ICompanyAccountService;
import cn.lls.service.ICompanyAllInfoService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  公司基本信息控制器
 * </p>
 *
 * @author 划水艺术家
 * @since 2022-11-15
 */
@RestController
@RequestMapping("/company/base")
@Slf4j
@Api(tags = "公司基本信息控制器")
public class CompanyBaseController {

    @Autowired
    private ICompanyAllInfoService companyAllInfoService;


    @Autowired
    private ICompanyAccountService companyAccountService;


    @GetMapping("/{companyId}")
    public R getCompanyById(@PathVariable("companyId") Long companyId){
        return R.success(companyAllInfoService.getCompanyById(companyId));
    }

    @PutMapping
    @ApiOperation(value = "公司修改", notes = "公司修改", response = R.class, httpMethod = "POST")
    public R companyUpdate(
            @ApiParam(required = true, value = "公司实体信息")
            @RequestBody @Validated CompanyReq companyReq,
            BindingResult bindingResult) {
        companyAllInfoService.companyUpdate(companyReq);
        return R.success();
    }

    @PutMapping("/pwd")
    public R companyUpdatePwd(@RequestBody  CAAccountReq caAccountReq) {
        LambdaUpdateWrapper<CompanyAccountEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CompanyAccountEntity::getCompanyId, caAccountReq.getCompanyId());
        CompanyAccountEntity companyAccountEntity = new CompanyAccountEntity();
        BeanUtils.copyProperties(caAccountReq, companyAccountEntity);
        companyAccountEntity.setLoginPassword(MD5Util.MD5Encode(caAccountReq.getLoginPassword()));
        companyAccountService.update(companyAccountEntity, updateWrapper);
        return R.success();

    }

}

